package com.yecheng.yeaiagent.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import com.yecheng.yeaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

public class FileOperationsTools {

    @Tool(description = "Read content from a file")
    public String readFile(@ToolParam(description = "Name of the file to read")
                           String fileName){
        try {
            String filePath = FileConstant.FILE_SAVE_DIR + "/" + fileName;
            return FileUtil.readUtf8String(filePath);
        } catch (IORuntimeException e) {
            return "Error reading file: " + e.getMessage();
        }
    }

    @Tool(description = "Write content to the file")
    public String writeFile(@ToolParam(description = "Name of the file to write")
                            String fileName,
                            @ToolParam(description = "Content to write to the file")
                            String content){
        String filePath = FileConstant.FILE_SAVE_DIR + "/" + fileName;
        try {
            FileUtil.mkdir(FileConstant.FILE_SAVE_DIR);
            FileUtil.writeUtf8String(content,filePath);
            return "write success to " + fileName;
        } catch (IORuntimeException e) {
            return "Error writing file: " + e.getMessage();
        }
    }
}
